package com.oocl.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.oocl.dao.FoodAdDao;
import com.oocl.dao.FoodsDao;
import com.oocl.dao.MerchantsDao;
import com.oocl.dao.base.BaseDao;
import com.oocl.entity.FoodAd;
import com.oocl.entity.Foods;
import com.oocl.entity.Merchants;
import com.oocl.service.FoodAdService;
import com.oocl.util.VoUtil;
import com.oocl.vo.FoodAdVo;

@Service(value="foodAdService")
public class FoodAdServiceImpl implements FoodAdService {

	@Resource(name="foodAdDao")
	private FoodAdDao fad;
	@Resource(name="baseDao")
	private BaseDao baseDao;
	@Resource(name="foodsDao")
	private FoodsDao foodsDao;
	@Resource(name="merchantsDao")
	private MerchantsDao merchantsDao;

	@Transactional
	public FoodAdVo addFoodAd(String fid,String icnum) {
		Foods food=(Foods)baseDao.find(Foods.class, fid);
		Merchants merchant=merchantsDao.findmerchantsByIcnum(icnum);
		FoodAd  foodAd=new FoodAd(null, food, merchant, "2", new Date(), null);
		return VoUtil.translate(FoodAdVo.class,(FoodAd)baseDao.persist(foodAd));
	}

	public List<FoodAdVo> findFoodAdByMerchant(String icnum) {
		
		return VoUtil.translateList(FoodAdVo.class, fad.findFoodAdsByMerchant(icnum));
	}

	public List<FoodAdVo> findPassedFoodAdByMerchant(String icnum) {
		List<FoodAd> fList=new ArrayList<FoodAd>();
		for (FoodAd f:fad.findFoodAdsByMerchant(icnum)) {
			if (f.getStatus().equals("1")) {
				fList.add(f);
			}
		}
		System.out.println(fList.size());
		return VoUtil.translateList(FoodAdVo.class, fList);
	}

	public List<FoodAdVo> findUnpassedFoodAdByMerchant(String icnum) {
		List<FoodAd> fList=new ArrayList<FoodAd>();
		for (FoodAd f:fad.findFoodAdsByMerchant(icnum)) {
			if (f.getStatus().equals("2")) {
				fList.add(f);
			}
		}
		return VoUtil.translateList(FoodAdVo.class, fList);
	}

	
	@Transactional
	public void  updateAdStatus(String foodName,String icnum,String status) {
		FoodAd fa = fad.getFoodsAdByFoodNameIcn(foodName,icnum);
		System.out.println("fa.getId()  :"+fa.getId());
		if("1".equals(status.trim())){
			fa.setStatus("1");			
		}else{
			fa.setStatus("2");			
		}
		fa.setAutitTime(new Date());
		baseDao.update(fa);
	}

}
